﻿#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2008-10-20 - Created
 */
#endregion

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using IQ.DbBase;

namespace SqlServerAdmin
{
    public class SqlMdText : SqlMetaData
    {
        #region # Constructor #
        public SqlMdText() : base()
        {
        }
        #endregion

        #region - Private variables -
        private DataTable dtServer;
        private DataTable dtServerSpTxt; // Sp, Views, Functions
        #endregion

        #region - Private methods -
        private string getSpTextFromDataRows(DataRow[] rows)
        {
            if (rows != null && rows.Length > 0)
            {
                if (rows.Length == 1)
                    return rows[0][SqlQueries.Field_ObjectText].ToString();
                else
                {
                    string tmpText = rows[0][SqlQueries.Field_ObjectText].ToString();
                    for (int i = 1; i < rows.Length; ++i)
                        tmpText += rows[i][SqlQueries.Field_ObjectText].ToString();
                    return tmpText;
                }
            }
            else
                return "";
        }
        #endregion

        #region - Protected variables -
        #endregion

        #region - Protected methods -
        protected override void loadServerData()
        {
            dtServer = DbHelper.Load_Table(SqlQueries.GetSqlMdSystemObjects(SqlQueries.SoT_StoreProc));
            dtServerSpTxt = DbHelper.Load_Table(SqlQueries.GetSqlMd_SVF());
        }
        #endregion

        #region - Public properties / variables -
        public DataTable FileStoreProc
        {
            get { return SqlMdFileData.Instance.FileStoreProc; }
        }
        public DataTable ServerSp
        {
            get { return dtServer; }
        }
        #endregion

        #region - Public methods -
        public string GetFileObjectText(string nameType, string name)
        {
            return SqlMdFileData.Instance.GetFileObjectText(nameType, name);
        }
        public string GetServerObjectText(string nameType, string name)
        {
            DataRow[] drData = dtServerSpTxt.Select(SqlQueries.Field_Type + "='" + nameType + "' AND " +
                 SqlQueries.Field_Name + "='" + name + "'");
            if (drData != null && drData.Length > 0)
            {
                //string serverId = serverData[0]["id"].ToString();
                //DataRow[] spServer = dtServerSpTxt.Select("ID='" + serverId + "'");
                return getSpTextFromDataRows(drData);
            }
            return "";
        }
        public void CompareAll_Sp()
        {
            compareAll(SqlQueries.SoT_StoreProc, CompareObjectText);
        }
        public bool CompareObjectText(string nameType, string name)
        {
            string serverSpText = this.GetServerObjectText(nameType, name);
            if (!String.IsNullOrEmpty(serverSpText))
            {
                string fileSp = GetFileObjectText(nameType, name);
                if (fileSp != serverSpText)
                {
                    addToChanges(name, ChangeType.Changed);
                    return true;
                }
                else
                    return false;
            }
            else
            {
                addToChanges(name, ChangeType.NotExistOnServer);
                return true;
            }
        }
        public void UpdateServerObjectText(string nameType, string name)
        {
            DataRow dr = dtChanges.Select(SqlQueries.Field_Name + "='" + name + "'").FirstOrDefault();
            if (dr != null)
            {
                string fileText = GetFileObjectText(nameType, name);
                if (fileText.Length > 0)
                {
                    if ((ChangeType)dr["ChangeType"] == ChangeType.Changed)
                    {
                        fileText = fileText.Replace("CREATE ", "ALTER ");
                    }
                    try
                    {
                        DbHelper.ExecuteSql(fileText);
                        dtChanges.Rows.Remove(dr);
                    }
                    catch (Exception err)
                    {
                        throw err;
                    }
                }
            }
        }
        public void DeleteServerText(string nameType, string name)
        {
            DataRow dr = dtChanges.Select(SqlQueries.Field_Name + "='" + name + "'").FirstOrDefault();
            if (dr != null)
            {
                string typeObj = "";
                switch (nameType.ToUpper())
                {
                    case SqlQueries.SoT_Tables: typeObj = "TABLE"; break;
                    case SqlQueries.SoT_Triggers: typeObj = "TRIGGER"; break;
                    case SqlQueries.SoT_StoreProc: typeObj = "PROCEDURE"; break;
                    case SqlQueries.SoT_ForeignKey:
                    case SqlQueries.SoT_PrimaryKey: typeObj = "CONSTRAINT"; break;
                    case SqlQueries.SoT_Functions:
                    case SqlQueries.SoT_FunctionsAsm: typeObj = "FUNCTION"; break;
                    case SqlQueries.SoT_UniqueIndex: typeObj = "INDEX"; break;
                    case SqlQueries.SoT_View: typeObj = "VIEW"; break;
                }
                try
                {
                    DbHelper.ExecuteSql("DROP " + typeObj + " " + name);
                    dtChanges.Rows.Remove(dr);
                }
                catch (Exception err)
                {
                    throw err;
                }
            }
        }
        #endregion
    }
}
